package com.xdap.table.controller;

import com.definesys.mpaas.common.http.Response;
import com.xdap.table.service.InitTableColumnService;
import com.xdap.table.service.InitTableDetailRelService;
import com.xdap.table.service.InitTableRelService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @auth lishushu
 * @2023/4/7 11:26
 * @desc {生成业务表之间的关系的excel}
 **/
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/df/table/excel")
@Slf4j
public class TableExcelConController {

    @Autowired
    private InitTableRelService initTableRelService;
    @Autowired
    private InitTableDetailRelService initTableDetailRelService;
    @Autowired
    private InitTableColumnService initTableColumnService;


    /**
     * @auth lishushu
     * @2023/4/19 14:20
     * @desc {生成对应的表关系}
     **/
    @PostMapping("/createTableRel")
    public Response target(@RequestParam("filePath") String filePath, @RequestParam("formId") String formId) {
        Response res = new Response();
        try {
            String result = initTableRelService.init(filePath, formId);
            res.setData(result);
        } catch (Exception e) {
            log.error("初始化持久层报错:{}", e);
            res.setCode(Response.CODE_ERR);
            res.setMessage(e.getMessage());
        }
        return res;
    }

    /**
     * @auth lishushu
     * @2023/4/19 14:20
     * @desc {生成对应的表关系}
     **/
    @PostMapping("/createTableDetailRel")
    public Response createTableDetailRel(@RequestParam("filePath") String filePath, @RequestParam("formId") String formId) {
        Response res = new Response();
        try {

            String result = initTableDetailRelService.initDetail(filePath, formId);
            res.setData(result);
        } catch (Exception e) {
            log.error("初始化持久层报错:{}", e);
            res.setCode(Response.CODE_ERR);
            res.setMessage(e.getMessage());
        }
        return res;
    }

    /**
     * @auth lishushu
     * @2023/4/19 14:20
     * @desc {生成对应的表关系}
     **/
    @PostMapping("/createTableColumn")
    public Response createTableColumn(@RequestParam("filePath") String filePath, @RequestParam("formId") String formId) {
        Response res = new Response();
        try {
            String result = initTableColumnService.init(filePath, formId);
            res.setData(result);
        } catch (Exception e) {
            log.error("初始化持久层报错:{}", e);
            res.setCode(Response.CODE_ERR);
            res.setMessage(e.getMessage());
        }
        return res;
    }



}
